/**
 * 
 */
package org.swing.utility.jai.file;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Quynh Nhu
 *
 */
public class FileInfo implements Comparable {
	  private String fileName;

	  private long fileSize;

	  private String fileType;

	  private String name;

	  private long lastModified;

	  public FileInfo(final File file) {
	    fileName = file.getName();
	    int extensionIndex = fileName.lastIndexOf('.');
	    if (extensionIndex > -1) {
	      name = fileName.substring(0, extensionIndex);
	      fileType = fileName.substring(extensionIndex + 1);
	    } else {
	      name = fileName;
	      fileType = "";
	    }
	    fileSize = file.length();
	    lastModified = file.lastModified();
	  }

	  public String getFileName() {
	    return fileName;
	  }

	  public long getFileSize() {
	    return fileSize;
	  }

	  public String getFileSizeString() {
	    if (fileSize < 1024) {
	      return fileSize + "B";
	    } else if (fileSize < 1048576) {
	      return fileSize / 1024 + "KB";
	    } else {
	      return fileSize / 1048576 + "KB";
	    }
	  }

	  public String getFileType() {
	    return fileType;
	  }

	  public String getName() {
	    return name;
	  }

	  public String toString() {
	    return fileName.toString();
	  }

	  public static List getFileInfo(final File[] files) {
	    ArrayList fileInfos = new ArrayList();
	    for (int i = 0; i < files.length; i++) {
	      File file = files[i];
	      fileInfos.add(new FileInfo(file));
	    }
	    Collections.sort(fileInfos);
	    return fileInfos;
	  }

	  public Date getLastModified() {
	    return new Date(lastModified);
	  }

	  public int compareTo(final Object o) {
	    if (o instanceof FileInfo) {
	      FileInfo file = (FileInfo)o;
	      return fileName.compareTo(file.fileName);
	    }
	    return -1;
	  }
	}
